/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.xuidemo.slice.edittext;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;

import com.xuexiang.xui_lib.component.edittext.ValidatorEditText;
import com.xuexiang.xui_lib.component.edittext.materialedittext.MaterialEditText;
import com.xuexiang.xui_lib.component.edittext.materialedittext.validation.RegexpValidator;
import com.xuexiang.xuidemo.ResourceTable;

/**
 * 统一的输入框样式
 *
 * @since 2021-04-28
 */
public class EditStyleAbilitySlice extends AbilitySlice {
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ablility_style_edit);
        initView();
    }

    /**
     * 初始化
     */
    public void initView() {
        findComponentById(ResourceTable.Id_MaterialEditText_backImage).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminate();
            }
        });
        MaterialEditText materialEditText1 = (MaterialEditText)
            findComponentById(ResourceTable.Id_MaterialEditText_enabled);
        Button button1 = (Button) findComponentById(ResourceTable.Id_button_enabled);
        button1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                materialEditText1.setInputEnable(!materialEditText1.isEnabled());
                button1.setText(materialEditText1.isEnabled() ? "不允许输入" : "允许输入");
            }
        });
        MaterialEditText materialEditText2 = (MaterialEditText)
            findComponentById(ResourceTable.Id_MaterialEditText_validate1);
        materialEditText2.addValidator(new RegexpValidator("只能输入数字!", "\\d+"));
        Button button2 = (Button) findComponentById(ResourceTable.Id_button_validate);
        button2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                materialEditText2.validate();
            }
        });
        MaterialEditText materialEditText3 = (MaterialEditText)
            findComponentById(ResourceTable.Id_MaterialEditText_validate2);
        materialEditText3.addValidator(new RegexpValidator("只能输入数字!", "\\d+"));
        materialEditText3.addValidator(new RegexpValidator("数字必须大于0小于100!", "^[1-9]?\\d$"));
        materialEditText3.setAutoValidate(true);
        ValidatorEditText validatorEditText = (ValidatorEditText)
            findComponentById(ResourceTable.Id_ValidatorEditText_validate);
        validatorEditText.addValidator(new RegexpValidator("数字必须大于0小于100!", "^[1-9]?\\d$"));
        validatorEditText.setAutoValidate(true);
    }
}
